﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyPublisher.Service;
using System.ServiceModel;

namespace MyPublisher.Web
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BlogMenu.Items[0].Selected = true;
            }
        }

        protected void PrintButton_Click(object sender, ImageClickEventArgs e)
        {
            string userName = UserNameTextBox.Text;
            string passWord = PasswordTextBox.Text;
            string provider = BlogMenu.SelectedValue;

            PublishServiceClient client = new PublishServiceClient("Publish");
            try
            {
                client.Open();
                PublishInfo pi = new PublishInfo()
                {
                    BeginTime = DateTime.Now,
                    EndTime = DateTime.Now,
                    Password = passWord,
                    UserName = userName,
                    Provider = provider,
                };
                string documentId = client.Publish(pi);
                DownloadButton.Visible = true;
                DownloadButton.NavigateUrl = String.Format("Download.aspx?p={0}",documentId);
            }
            catch (Exception ex)
            {
                if (client.State == CommunicationState.Faulted)
                {
                    client.Abort();
                }
                else
                {
                    client.Close();
                }
            }
        }
    }
}
